<script>
import { createApp } from "vue";
import { createPinia } from "pinia";
import useUserStore from "@/store/modules/user/index.js";
import { scanCode } from "@/api/user";
const app = createApp({});
const pinia = createPinia();
app.use(pinia);
const { isLogin, setData } = useUserStore();
export default {
  onLaunch: function () {
    console.log("App Launch");
    let menuButtonInfo = uni.getMenuButtonBoundingClientRect();
    const navBarTop = menuButtonInfo.top;
    const navBarHeight = menuButtonInfo.height;
    const navBarInfo = {navBarTop,navBarHeight}
    uni.setStorageSync("navBarInfoStorage", JSON.stringify(navBarInfo));
  },
  onShow: async function (options) {
    const from = wx.getEnterOptionsSync().scene;
    const token = uni.getStorageSync("token");
    if (from == 1047) {
      // 扫描小程序码
      if (options.query?.scene) {
        // clear start重新扫码进入之前应该清除上次扫码存储的内容
        uni.removeStorageSync("tenantId");
        uni.removeStorageSync("userInfo");
        // clear end
        const scene = options.query?.scene;
        uni.setStorageSync("tenantId", scene);
        uni.setStorageSync("isScan", 1);
        // 未登录
        if (!token || !token.value) {
          isLogin.popShow = true;
        } else {
          // 已登录 扫码
          try {
            const res = await scanCode({
              tenantId: scene,
            });
            if (res?.code == 200) {
              let data = res?.data;
              console.log('已登录 扫码data', data)
              setData(data);
              uni.removeStorageSync("isScan");
            }
          } catch (error) {
            isLogin.popShow = true;
            console.log(error);
          }
        }
      }
    } else if (from == 1089) {
      // 微信下拉进入
    }
  },
  onHide: function () {
    console.log("App Hide");
    const token = uni.getStorageSync("token");
    if (!token) {
      // 扫小程序进来后（带了租户id）没登陆，退掉重新进来，清除租户id
      uni.removeStorageSync("tenantId");
    }
  },
};
</script>

<style lang="scss">
/*每个页面公共css */
page {
  background: #f8faff;

  :deep(.wd-tag) {
    height: 40rpx;
    display: inline-flex !important;
    align-items: center;
    justify-content: center;
    font-size: 20rpx;
  }
}

@import "@/static/font/font.css";
@import "@/static/style/common.scss";
@import "@/style/common.scss";
</style>
